//
//  CNAddNewAddressViewController.m
//  BeeQuick_19
//
//  Created by 赵玉忠 on 16/9/24.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNAddNewAddressViewController.h"
#import "UIBarButtonItem+CNAddNewAddressUIBarButtonItem.h"
#import "UIView+CNAddNewAddressViewCategory.h"
#import "RSChooseCityPickerView.h"
#import "CNConsigneeModel.h"
#import "CNSex.h"
@interface CNAddNewAddressViewController ()
@property (nonatomic,weak) UIButton *selectCityButton;
@property (nonatomic,weak) UITextField *addressViewButton;
@property (nonatomic,weak) UITextField *contectTextField;
@property (nonatomic,weak) UITextField *telephoneTextField;
@property (nonatomic,weak) UITextField *doorTextField;

@property (nonatomic,weak) CNSex *sexView;

@property (nonatomic,strong) CNConsigneeModel *model;


@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *telephone;
@property (nonatomic,copy) NSString *address;
@property (nonatomic,copy) NSString *sex;

@end

@implementation CNAddNewAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
    self.navigationItem.title = @"新增地址";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem loadNewUIBarButtonItemWithButtonName:@"保存" withUIColor:[UIColor darkGrayColor] andWithTarget:self andAction:@selector(saveButton)];
    
    //注册通知
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MAN:) name:@"MAN" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WOMAN:) name:@"WOMAN" object:nil];
    [self setupUI];
}
#pragma mark - 通知调用的方法
- (void)MAN:(NSNotification *)text{
    NSLog(@"%@",text.userInfo[@"MAN"]);
    self.sex = text.userInfo[@"MAN"];
    NSLog(@"%@",self.sex);
    NSLog(@"－－－－－接收到通知------");
}
- (void)WOMAN:(NSNotification *)text{
    NSLog(@"%@",text.userInfo[@"WOMAN"]);
    self.sex = text.userInfo[@"MAN"];
    NSLog(@"%@",self.sex);
    NSLog(@"－－－－－接收到通知------");
}

#pragma mark - 移除通知
-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MAN" object:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"WOMAN" object:self];
}
#pragma mark - 显示编辑数据
-(void)viewWillAppear:(BOOL)animated{
    if (self.tag == 1) {
        CNConsigneeModel *model = self.consigneeAddressModelArray[self.modelIndex];
        self.contectTextField.text = model.accept_name;
        self.telephoneTextField.text = model.telphone;
        self.doorTextField.text = model.addr_for_dealer;
        self.selectCityButton.titleLabel.text = model.city_name;
        [self.selectCityButton setTitle:model.city_name forState:UIControlStateNormal];
        [self.selectCityButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [self.addressViewButton setTitle:model.address forState:UIControlStateNormal];
        self.addressViewButton.text = model.address;
//        [self.addressViewButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [self.sexView chooseSex:[model.gender intValue]];
    }
}
-(void)setupUI{
    //联系人
    UIView *contectView = [UIView basicViewWithTitle:@" 联系人"];
    contectView.backgroundColor = [UIColor whiteColor];
    UITextField *connectionTextField = [UITextField new];
    self.contectTextField = connectionTextField;
    connectionTextField.font = [UIFont systemFontOfSize:15];
    [connectionTextField addTarget:self action:@selector(contectTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    connectionTextField.placeholder = @"收货人姓名";
    [contectView addSubview:connectionTextField];
    [connectionTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contectView).offset(85);
        make.right.equalTo(contectView);
        make.top.bottom.equalTo(contectView);
    }];
    [self.view addSubview:contectView];
    [contectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
        make.height.mas_equalTo(50);
    }];
    
    //性别选择
    CNSex *sexView = [[NSBundle mainBundle]loadNibNamed:@"CNSex" owner:nil options:nil].firstObject;
    self.sexView = sexView;
    [self.view addSubview:sexView];
    [sexView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contectView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.offset(50);
    }];
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
    [sexView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(sexView);
        make.height.offset(1);
    }];

    //手机号码
    UIView *telephoneView = [UIView basicViewWithTitle:@"手机号码"];
    telephoneView.backgroundColor = [UIColor whiteColor];
    UITextField *telephoneViewTextField = [UITextField new];
    telephoneViewTextField.font = [UIFont systemFontOfSize:15];
    self.telephoneTextField = telephoneViewTextField;
    [telephoneViewTextField addTarget:self action:@selector(telePhoneTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    telephoneViewTextField.placeholder = @"您的联系方式";
    [telephoneView addSubview:telephoneViewTextField];
    [telephoneViewTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(telephoneView).offset(85);
        make.right.top.bottom.equalTo(telephoneView);
    }];
    [self.view addSubview:telephoneView];
    [telephoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(sexView.mas_bottom);
        make.height.offset(50);
    }];
    
    //所在城市
    UIView *cityView = [UIView basicViewWithTitle:@"所在城市"];
    cityView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:cityView];
    [cityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(telephoneView.mas_bottom);
        make.height.offset(50);
    }];
    UIButton *cityViewButton = [UIButton new];
    self.selectCityButton = cityViewButton;
    cityViewButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [cityViewButton setTitle:@"请选择城市" forState:UIControlStateNormal];
    [cityViewButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    cityViewButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [cityViewButton addTarget:self action:@selector(chooseCity) forControlEvents:UIControlEventTouchUpInside];
    [cityView addSubview:cityViewButton];
    [cityViewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cityView).offset(85);
        make.right.top.bottom.equalTo(cityView);
    }];
    
    //收货地址 请选择您的住宅小区，大厦或学校
    UIView *addressView = [UIView basicViewWithTitle:@"收货地址"];
    addressView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:addressView];
    [addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(cityView.mas_bottom);
        make.height.offset(50);
    }];
    UITextField *addressViewButton = [UITextField new];
    self.addressViewButton = addressViewButton;
    addressViewButton.font = [UIFont systemFontOfSize:15];
    self.addressViewButton = addressViewButton;
    addressViewButton.placeholder = @"请选择您的住宅小区，大厦或学校";
    addressViewButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    [addressViewButton addTarget:self action:@selector(mapAddress) forControlEvents:UIControlEventTouchUpInside];
    [addressView addSubview:addressViewButton];
    [addressViewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addressView).offset(85);
        make.right.top.bottom.equalTo(addressView);
    }];
    
    //门牌号
    UIView *doorView = [UIView basicViewWithTitle:@" 门牌号"];
    doorView.backgroundColor = [UIColor whiteColor];
    UITextField *doorViewTextField = [UITextField new];
    doorViewTextField.font = [UIFont systemFontOfSize:15];
    self.doorTextField = doorViewTextField;
    doorViewTextField.placeholder = @"请输入楼号门牌号等详细信息";
    [doorViewTextField addTarget:self action:@selector(addressTextFieldDidChange :) forControlEvents:UIControlEventEditingChanged];
    [doorView addSubview:doorViewTextField];
    [doorViewTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(doorView).offset(85);
        make.right.equalTo(doorView);
        make.top.bottom.equalTo(doorView);
    }];
    [self.view addSubview:doorView];
    [doorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(addressView.mas_bottom);
        make.height.mas_equalTo(50);
    }];

}

#pragma mark - 选择城市
-(void)chooseCity{
    //回收键盘
//    // 1.找到弹出键盘的textField
//    UITextField *textField = (UITextField *)[self.view.window viewWithTag:10000];
//    // 2.回收键盘
//    [textField resignFirstResponder];
    [self reKeyBoard];
    //弹出第三方框架的城市选择器
    RSChooseCityPickerView  * cityPickerView    =   [RSChooseCityPickerView shareInstance];
    [cityPickerView showBottomView];
    [self.view   addSubview:cityPickerView];
    [cityPickerView setBlock:^(NSString *province, NSString *city, NSString *district){
        NSLog(@"%@--%@--%@",province,city,district);
        [self.selectCityButton setTitle:city forState:UIControlStateNormal];
        [self.selectCityButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.selectCityButton.titleLabel.font = [UIFont systemFontOfSize:15];
    }];
}
#pragma mark - 收货地址
-(void)mapAddress{
    //弹出地图搜索地址
    NSLog(@"弹出地图搜索地址");
}
#pragma mark - 监听textFeild的text
-(void)contectTextFieldDidChange :(UITextField *)theTextField{
    self.name = theTextField.text;
    
    
}
-(void)telePhoneTextFieldDidChange :(UITextField *)theTextField{
    self.telephone = theTextField.text;
    
}
-(void)addressTextFieldDidChange :(UITextField *)theTextField{
    self.address = theTextField.text;
}

//#pragma 保存新增
//-(void)viewWillDisappear:(BOOL)animated{
//    
//    
//
//}
#pragma mark - 监听保存按钮的点击
-(void)saveButton{
    
    self.model.accept_name = self.contectTextField.text;
    NSLog(@"%@",self.name);
    self.model.telphone = self.telephoneTextField.text;
    self.model.addr_for_dealer = self.doorTextField.text;
    self.model.gender = self.sex;
    self.model.city_name = self.selectCityButton.titleLabel.text;
    self.model.address = self.addressViewButton.text;
    NSLog(@"%@",self.model.gender);
    if (self.tag == 0) {
        [self.consigneeAddressModelArray addObject:self.model];
    }else{
        [self.consigneeAddressModelArray removeObjectAtIndex:self.modelIndex];
        NSLog(@"%ld",self.consigneeAddressModelArray.count);
        [self.consigneeAddressModelArray insertObject:self.model atIndex:self.modelIndex];
        NSLog(@"%ld",self.consigneeAddressModelArray.count);
        CNConsigneeModel *model = self.consigneeAddressModelArray[0];
        NSLog(@"%@%@%@%@",model.gender,model.accept_name,model.telphone,model.addr_for_dealer);
    }
    
    
    
    //归档
    // temp路径
    NSString* tempPath = NSTemporaryDirectory();
    
    // file路径
    NSString* filePath = [tempPath stringByAppendingPathComponent:@"shopCar.address"];
    
    // 归档
    [NSKeyedArchiver archiveRootObject:self.consigneeAddressModelArray toFile:filePath];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    NSLog(@"%ld",self.consigneeAddressModelArray.count);

}

#pragma mark - 懒加载一个模型
-(CNConsigneeModel *)model{
    if (_model == nil) {
//        static dispatch_once_t onceToken;
//        dispatch_once(&onceToken, ^{
            _model = [[CNConsigneeModel alloc]init];
//        });
    }
    return _model;
}

#pragma mark - 回收键盘
- (void)reKeyBoard
{
    [self.contectTextField resignFirstResponder];
    [self.telephoneTextField resignFirstResponder];
    [self.doorTextField resignFirstResponder];
    [self.addressViewButton resignFirstResponder];
}
@end


